package com.netease.nie.base.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.netease.nie.base.protocol.SystemResponseCode;

/**
 * 响应帮助类
 * 
 * @author KingsHunter
 * 
 * @date 2013年9月4日
 * 
 */
public class ProtocolUtils {

    /**
     * 组装json响应格式-格式1
     * 
     * @param code
     * @param message
     * @param data
     * @return
     */
    public static Map<String, Object> populateSimpleResponseJson(int code, String message, Object data) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("code", code);
        jsonMap.put("msg", StringUtils.isBlank(message) ? "Ok" : message);
        jsonMap.put("data", data);
        return jsonMap;
    }
    
    /**
     * 组装json响应格式-格式1
     * @param data
     * @return
     */
    public static String populateResponseJson(Object data) {
    	return populateResponseJson(SystemResponseCode.OK.getCode(), SystemResponseCode.OK.getMsg(), data);
    }
    
    /**
     * 组装json响应格式-格式2
     * @param msg
     * @param data
     * @return
     */
    public static String populateResponseJson(String message, Object data) {
    	if (StringUtils.isBlank(message))
    		message = SystemResponseCode.OK.getMsg();
    	return populateResponseJson(SystemResponseCode.OK.getCode(), message, data);
    }

    /**
     * 组装json响应格式-格式3
     * 
     * @param code
     * @param message
     * @param data
     * @return
     */
    private static String populateResponseJson(int code, String message, Object data) {
    	Map<String, Object> jsonMap = new HashMap<String, Object>();
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("code", code);
        stateMap.put("msg", StringUtils.isBlank(message) ? SystemResponseCode.OK.getMsg() : message);
        jsonMap.put("state", stateMap);
        jsonMap.put("data", data);
        return JsonUtils.obj2String(jsonMap);
    }
    
    /**
     * 组装json响应格式-错误1
     * @param code
     * @return
     */
    public static String populateResponseJson() {
    	return populateResponseJson(SystemResponseCode.INTERNAL_ERROR.getCode(), SystemResponseCode.INTERNAL_ERROR.getMsg());
    }
    
    /**
     * 组装json响应格式-错误2
     * @param code
     * @return
     */
    public static String populateResponseJson(int code) {
    	SystemResponseCode[] systemResponseCodes = SystemResponseCode.values();
    	for (SystemResponseCode systemResponseCode : systemResponseCodes) {
    		if (code == systemResponseCode.getCode())
    			return populateResponseJson(code, systemResponseCode.getMsg());
    	}
    	return populateResponseJson();
    }
    
    /**
     * 组装json响应格式-错误3
     * @param code
     * @param message
     * @return
     */
    public static String populateResponseJson(int code, String message) {
    	Map<String, Object> jsonMap = new HashMap<String, Object>();
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("code", code);
        stateMap.put("msg", message);
        jsonMap.put("state", stateMap);
        return JsonUtils.obj2String(jsonMap);
    }

}
